package disenioPantallas.ActividadesCampo;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import disenioPantallas.elementos.EButton;
import disenioPantallas.elementos.EPanel;
import disenioPantallas.elementos.dialogGeneral;
import gestores.GestorCampo;
import gestores.GestorErrores;

import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;

import actionListeners.MascaraMayusculakeyListener;



public class DCaracteristicasCampo extends dialogGeneral {

	private static final long serialVersionUID = 1L;
	private GestorCampo gCampo=GestorCampo.getGestorCampo();
	private JTextArea textArea;

	
	public DCaracteristicasCampo() {
		setTitle(gCampo.getCampoActual().getNombreCampo());
		setMinimumSize(new Dimension(500, 200));
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		Component verticalStrut = Box.createVerticalStrut(8);
		getContentPane().add(verticalStrut);
		
		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane);
		scrollPane.setMaximumSize(new Dimension(450, 100));
		scrollPane.setBorder(new TitledBorder(null, "Caracateristicas", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		textArea.addKeyListener(new MascaraMayusculakeyListener());
		textArea.setText(gCampo.getCampoActual().getCaracteristicas());
		
		JPanel panelCancelar = new EPanel();
		getContentPane().add(panelCancelar);
		
		JButton btnAceptar = new EButton("Aceptar");
		panelCancelar.add(btnAceptar);
		btnAceptar.addActionListener(new aceptarAction());
		
		JButton btnCancelar = new EButton("Cancelar");
		panelCancelar.add(btnCancelar);
		btnCancelar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cerrarDialog();
			}
		});
	}
	
	private class aceptarAction extends AbstractAction{


		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent e) {
			
			if(!textArea.getText().equals("")){
				try {
					gCampo.setCaracteristicas(gCampo.getCampoActual().getIdCampo(),textArea.getText());
					gCampo.getCampoActual().setCaracteristicas(textArea.getText());
					GestorErrores.getGestorErrores().mostrarCartelAdvertencia("Se ha registrado correctamente");
					dispose();

				} catch (Throwable e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					GestorErrores.getGestorErrores().mostrarCartelError(e1.getMessage());
				}
				}
		
		}
	}
}